﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Reviewer.Entities;
using System.Data;
using System.Collections.ObjectModel;
using System.Globalization;
using Reviewer.Utilities.Data;
using MySql.Data.MySqlClient;
using Reviewer.Data.Bases;
using System.Data.SqlClient;

namespace Reviewer.Data
{
    /// <summary>
    /// News Repository
    /// </summary>
    [CLSCompliant(false)]
    public class FrontPageTopRepository : RepositoryCrudBase<FrontPageTop, Guid>
    {
        #region fields (private)
        private const string TABLE_NAME = "V2_ForsideTop";
        private static readonly CrudColumnInfo columnId = new CrudColumnInfo(0, "Id", MySqlDbType.VarChar, 36, true, false, false, false);
        private static readonly CrudColumnInfo columnTitle = new CrudColumnInfo(1, "Titel", MySqlDbType.VarChar, 255, false, false, true);
        private static readonly CrudColumnInfo columnLink = new CrudColumnInfo(2, "Link", MySqlDbType.VarChar, 255, false, false, true);
        private static readonly CrudColumnInfo columnText = new CrudColumnInfo(3, "Tekst", MySqlDbType.VarChar, 255, false, false, true);
        private static readonly CrudColumnInfo columnImageId = new CrudColumnInfo(4, "PicID", MySqlDbType.VarChar, 255, false, false, true);
        #endregion

        #region methods (public)
        public Collection<FrontPageTop> Select(int maxResults)
        {
            return base.SelectAll(maxResults, FrontPageTopRepository.columnId, SortOrder.Descending);
        }
        #endregion

        #region methods (protected)
        internal override CrudColumnInfo[] Columns
        {
            get
            {
                return new CrudColumnInfo[]{
                    FrontPageTopRepository.columnId
                    , FrontPageTopRepository.columnTitle
                    , FrontPageTopRepository.columnLink
                    , FrontPageTopRepository.columnText
                    , FrontPageTopRepository.columnImageId};
            }
        }

        protected override string TableName
        {
            get { return FrontPageTopRepository.TABLE_NAME; }
        }

        internal override object ColumnToValue(CrudColumnInfo info, FrontPageTop item)
        {
            if (info.Equals(FrontPageTopRepository.columnId)) return item.Id;
            else if (info.Equals(FrontPageTopRepository.columnTitle)) return item.Title;
            else if (info.Equals(FrontPageTopRepository.columnText)) return item.Text;
            else if (info.Equals(FrontPageTopRepository.columnLink)) return item.Link;
            else if (info.Equals(FrontPageTopRepository.columnImageId)) return item.ImageId;
            return null;
        }

        protected override FrontPageTop BuildItemFromReader(IDataReader reader)
        {
            FrontPageTop item = new FrontPageTop();
            if (reader != null)
            {
                item.Id = DataReaderUtility.GetValue(reader, reader.GetOrdinal(FrontPageTopRepository.columnId.Name), Guid.Empty);
                item.Title = DataReaderUtility.GetValue(reader, reader.GetOrdinal(FrontPageTopRepository.columnTitle.Name), String.Empty);
                item.Text = DataReaderUtility.GetValue(reader, reader.GetOrdinal(FrontPageTopRepository.columnText.Name), String.Empty);
                item.Link = DataReaderUtility.GetValue(reader, reader.GetOrdinal(FrontPageTopRepository.columnLink.Name), String.Empty);
                item.ImageId = DataReaderUtility.GetValue(reader, reader.GetOrdinal(FrontPageTopRepository.columnImageId.Name), (Int32?)null);
            }
            return item;
        }
        #endregion
    }
}
